//
//  WmcRecognitionDescView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/10/19.
//

import UIKit

class WmcRecognitionDescView: WmcBaseView {
    // Internal

    override func makeUI() {
        addSubview(horiContentView)
        addSubview(timeLabel)
        addSubview(companyLabel)
        addSubview(remarkLabel)
        addSubview(logoView)

        horiContentView.addSubview(countLabel)
        horiContentView.addSubview(lengthLabel)
        horiContentView.addSubview(statusLabel)

        let horiContentViewTop = isIphoneX ? 20 : 10
        horiContentView.snp.makeConstraints { make in
            make.top.equalTo(horiContentViewTop)
            make.height.equalTo(45)
            make.left.equalTo(24)
            make.height.equalTo(45)
        }

        countLabel.snp.makeConstraints { make in
            make.top.left.bottom.equalTo(0)
            make.width.lessThanOrEqualTo(100)
        }

        lengthLabel.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(countLabel.snp.right)
            make.bottom.equalTo(0)
            make.width.lessThanOrEqualTo(100)
        }

        statusLabel.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(lengthLabel.snp.right).offset(24).priority(.high)
            make.right.equalTo(-24)
            make.bottom.equalTo(0)
        }

        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(horiContentView.snp.bottom).offset(2)
            make.height.equalTo(21)
        }

        companyLabel.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(timeLabel.snp.bottom).offset(8)
            make.height.equalTo(21)
        }

        remarkLabel.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(companyLabel.snp.bottom).offset(8)
            make.height.equalTo(21)
            make.bottom.equalTo(-0)
        }
        
        logoView.snp.makeConstraints { make in
            make.width.height.equalTo(60)
            make.right.equalTo(-12)
            make.top.equalTo(12)
        }
    }
    
    func updateCount(_ count: Int) {
        countLabel.text = "\(count)根"
    }
    
    func setHiddenLogo(_ hidden: Bool) {
        if WmcPreferenceSettingsManager.shared.cameraSetting.hiddenOfficialMark {
            return
        }
        logoView.isHidden = hidden
    }

    func bindModel(_ model: WmcWatermarkDetailModel?) {
        guard let model = model else { return }

        var isShow = false
        var count: String = ""
        var length: String = ""

        model.fields.forEach { optionModel in
            let type = optionModel.fieldType

            if type == .results {
                if optionModel.isOpen {
                    isShow = true
                    count = optionModel.content
                } else {
                    countLabel.text = ""
                }
            } else if type == .length {
                if optionModel.isOpen {
                    isShow = true
                    length = optionModel.content
                } else {
                    lengthLabel.text = ""
                }
            } else if type == .single {
                if optionModel.isOpen {
                    isShow = true
                    statusLabel.text = optionModel.content
                } else {
                    statusLabel.text = ""
                }
            } else if type == .time {
                if optionModel.isOpen {
                    timeLabel.snp.updateConstraints { make in
                        make.top.equalTo(horiContentView.snp.bottom).offset(2)
                        make.height.equalTo(21)
                    }
                } else {
                    timeLabel.snp.updateConstraints { make in
                        make.top.equalTo(horiContentView.snp.bottom).offset(0)
                        make.height.equalTo(0)
                    }
                }
            } else if type == .text {
                if optionModel.isOpen {
                    companyLabel.text = "\(optionModel.title)：\(optionModel.content)"
                    companyLabel.snp.updateConstraints { make in
                        make.top.equalTo(timeLabel.snp.bottom).offset(8)
                        make.height.equalTo(21)
                    }
                } else {
                    companyLabel.snp.updateConstraints { make in
                        make.top.equalTo(timeLabel.snp.bottom).offset(0)
                        make.height.equalTo(0)
                    }
                }
            } else if type == .remark {
                if optionModel.isOpen {
                    remarkLabel.text = "\(optionModel.title)：\(optionModel.content)"
                    remarkLabel.snp.updateConstraints { make in
                        make.top.equalTo(companyLabel.snp.bottom).offset(4)
                        make.height.equalTo(21)
                    }
                } else {
                    remarkLabel.snp.updateConstraints { make in
                        make.top.equalTo(companyLabel.snp.bottom).offset(0)
                        make.height.equalTo(0)
                    }
                }
            }
        }

        if !count.isEmpty {
            countLabel.text = "\(count)根"
            if !length.isEmpty {
                lengthLabel.text = "/\(length)"
            } else {
                lengthLabel.text = ""
            }
        } else {
            if !length.isEmpty {
                lengthLabel.text = "\(length)"
            } else {
                lengthLabel.text = ""
            }
        }

        let horiContentHeight = isShow ? 45 : 0

        horiContentView.snp.updateConstraints { make in
            make.height.equalTo(horiContentHeight)
        }
    }

    // Private

    private let isIphoneX = Wmc_safeAreaBottom > 0

    private lazy var horiContentView: UIView = {
        let view = UIView()
        return view
    }()

    private lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 32, weight: .medium)
        label.textColor = .wmc_grayH2_323233
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        label.minimumScaleFactor = 0.5
        label.adjustsFontSizeToFitWidth = true
        return label
    }()

    private lazy var lengthLabel: UILabel = {
        let label = UILabel()
        label.font = 20.wmc_font
        label.textColor = .wmc_grayH2_323233
        label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return label
    }()

    private lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.font = 20.wmc_font
        label.textColor = .wmc_grayH2_323233
        label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        return label
    }()

    private lazy var timeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: UIFont.systemFont(ofSize: 15, weight: .regular), color: UIColor.wmc_grayH2_323233, alignment: .left)
        return label
    }()
    
    private lazy var companyLabel: UILabel = {
        let label = UILabel()
        label.font = 15.wmc_font
        label.textColor = .wmc_grayH2_323233
        return label
    }()

    private lazy var remarkLabel: UILabel = {
        let label = UILabel()
        label.font = 15.wmc_font
        label.textColor = .wmc_gray_595959
        return label
    }()
    
    private lazy var logoView: UIImageView = {
        let view = UIImageView(image: "recognition_logo".wmc_image)
        view.isHidden = true
        return view
    }()
}
